home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dmake / depend.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  6KB  |  255 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  DEPEND.C
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. Prototype void InitDep(void);
  14. Prototype DepRef  *CreateDepRef(List *, char *);
  15. Prototype DepCmdList *AllocDepCmdList(void);
  16. Prototype DepRef  *DupDepRef(DepRef *);
  17. Prototype void      IncorporateDependency(DepRef *, DepRef *, List *);
  18. Prototype int      ExecuteDependency(DepRef *, time_t *);
  19.  
  20. Prototype List DepList;
  21.  
  22. List DepList;
  23.  
  24. void
  25. InitDep(void)
  26. {
  27.     NewList(&DepList);        /*    master list */
  28. }
  29.  
  30. DepRef *
  31. CreateDepRef(list, name)
  32. List *list;
  33. char *name;
  34. {
  35.     DepRef *ref;
  36.     DepNode *dep;
  37.  
  38.     for (dep = GetTail(&DepList); dep; dep = GetPred(&dep->dn_Node)) {
  39.     if (strcmp(name, dep->dn_Node.ln_Name) == 0)
  40.         break;
  41.     }
  42.     if (dep == NULL) {
  43.     dep = malloc(sizeof(DepNode) + strlen(name) + 1);
  44.     clrmem(dep, sizeof(DepNode));
  45.     dep->dn_Node.ln_Name = (char *)(dep + 1);
  46.     NewList(&dep->dn_DepCmdList);
  47.     strcpy(dep->dn_Node.ln_Name, name);
  48.     AddTail(&DepList, &dep->dn_Node);
  49.     }
  50.  
  51.     ref = malloc(sizeof(DepRef));
  52.     clrmem(ref, sizeof(DepRef));
  53.  
  54.     ref->rn_Node.ln_Name = dep->dn_Node.ln_Name;
  55.     ref->rn_Dep = dep;
  56.     AddTail(list, &ref->rn_Node);
  57.     return(ref);
  58. }
  59.  
  60.  
  61. DepRef *
  62. DupDepRef(ref0)
  63. DepRef *ref0;
  64. {
  65.     DepRef *ref = malloc(sizeof(DepRef));
  66.  
  67.     clrmem(ref, sizeof(DepRef));
  68.     ref->rn_Node.ln_Name = ref0->rn_Node.ln_Name;
  69.     ref->rn_Dep = ref0->rn_Dep;
  70.     return(ref);
  71. }
  72.  
  73. void
  74. IncorporateDependency(lhs, rhs, cmdList)
  75. DepRef *lhs;
  76. DepRef *rhs;
  77. List *cmdList;
  78. {
  79.     DepNode *dep = lhs->rn_Dep;     /*    source master */
  80.     DepCmdList *depCmdList = GetHead(&dep->dn_DepCmdList);
  81.  
  82.     if (depCmdList == NULL || depCmdList->dc_CmdList != cmdList) {
  83.     depCmdList = malloc(sizeof(DepCmdList));
  84.     clrmem(depCmdList, sizeof(DepCmdList));
  85.     NewList(&depCmdList->dc_RhsList);
  86.     depCmdList->dc_CmdList = cmdList;
  87.     AddHead(&dep->dn_DepCmdList, &depCmdList->dc_Node);
  88.     }
  89.  
  90.     if (rhs)
  91.     AddTail(&depCmdList->dc_RhsList, &rhs->rn_Node);
  92.  
  93.     db3printf(("Incorporate: %s -> %s\n", dep->dn_Node.ln_Name, (rhs) ? rhs->rn_Node.ln_Name : ""));
  94.  
  95. }
  96.  
  97. /*
  98.  *  Execute dependency returning an error and time completion code (time
  99.  *  completion code is 0 if object does not exit or had to be 'run')
  100.  */
  101.  
  102. int
  103. ExecuteDependency(ref, pt)
  104. DepRef *ref;
  105. time_t *pt;
  106. {
  107.     DepNode *dep = ref->rn_Dep;
  108.     int r = 0;
  109.  
  110.     dbprintf(("Go %s:\n", dep->dn_Node.ln_Name));
  111.  
  112.     if (dep->dn_Node.ln_Type != NT_RESOLVED) {
  113.     DepCmdList *depCmdList;
  114.     short statok = 0;
  115.     short masterForce = 0;
  116.     struct stat sbuf;
  117.  
  118.     dep->dn_Node.ln_Type = NT_RESOLVED;
  119.  
  120.     /*
  121.      *  sub-dependency groups are handled individually
  122.      */
  123.  
  124.     if (stat(dep->dn_Node.ln_Name, &sbuf) == 0) {
  125.         statok = 1;
  126.         dep->dn_Time = sbuf.st_mtime;
  127.     }
  128.  
  129.     for (depCmdList = GetHead(&dep->dn_DepCmdList); r == 0 && depCmdList; depCmdList = GetSucc(&depCmdList->dc_Node)) {
  130.         short force;
  131.  
  132.         /*
  133.          *    A lower level dependency that gets hit but has no command
  134.          *    list will force the next higher level dependency to get
  135.          *    hit.
  136.          */
  137.  
  138.         if (masterForce == 2) {
  139.         masterForce = 1;
  140.         force = 1;
  141.         } else {
  142.         force = DoAll;
  143.         }
  144.  
  145.         *pt = 0;
  146.  
  147.         for (ref = GetHead(&depCmdList->dc_RhsList); r == 0 && ref; ref = GetSucc(&ref->rn_Node)) {
  148.         time_t t;
  149.  
  150.         if ((r = ExecuteDependency(ref, &t)) < 0)
  151.             break;
  152.         if (t == 0)
  153.             force = 1;
  154.         if (*pt == 0 || (long)(t - *pt) > 0)
  155.             *pt = t;        /*    latest    */
  156.         dbprintf(("LHS %s stok=%d rhs=%s time=%08lx\n",
  157.             dep->dn_Node.ln_Name,
  158.             statok,
  159.             ref->rn_Node.ln_Name,
  160.             t
  161.             ));
  162.         }
  163.         if (r == 0) {
  164.         if (statok) {
  165.             /*
  166.              *    if the file exists check the time agains the
  167.              *    collected sub-dependency/  If the sub-dep is
  168.              *    newer we force
  169.              *
  170.              *    if *pt is NULL we check to see if there were any
  171.              *    sub-dependancies or commands.  If so we force, other
  172.              *    wise we load *pt with the file time
  173.              */
  174.  
  175.             if (*pt) {
  176.             if ((long)(*pt - sbuf.st_mtime) > 0)
  177.                 force = 1;
  178.             } else if (GetHead(&depCmdList->dc_RhsList) || GetHead(depCmdList->dc_CmdList)) {
  179.             force = 1;
  180.             } else {
  181.             *pt = sbuf.st_mtime;
  182.  
  183.             }
  184.         } else {
  185.             /*
  186.              *
  187.              */
  188.                     /* Who knows why this code is here.  Matt seems to remember */
  189.                     /* a bug that had existed at one time.  This probably used  */
  190.                     /* to say something different.                              */
  191.             /* if (GetHead(&depCmdList->dc_RhsList)) { ********DEAD******/
  192.             force = 1;
  193.             /* } else {                                ********DEAD******/
  194.             /* force = 1;                              ********DEAD******/
  195.             /* }                                       ********DEAD******/
  196.         }
  197.  
  198.         dbprintf(("LHS %s stok=%d time=%08lx force= %d\n",
  199.             dep->dn_Node.ln_Name,
  200.             statok,
  201.             *pt,
  202.             force
  203.             ));
  204.  
  205.         /*
  206.          *  run command list if necessary.  If run then force result
  207.          *  to caller by setting *pt to 0
  208.          *
  209.          *  [re]create %(left) and %(right) variables
  210.          */
  211.  
  212.         if (force) {
  213.             dbprintf(("FORCE %s\n", dep->dn_Node.ln_Name));
  214.  
  215.             masterForce = 1;
  216.             if (GetHead(depCmdList->dc_CmdList)) {
  217.             Var *var;
  218.  
  219.             if ((var = MakeVar("left", '%')) != NULL) {
  220.                 PutCmdListSym(&var->var_CmdList, dep->dn_Node.ln_Name, NULL);
  221.             }
  222.             if ((var = MakeVar("right", '%')) != NULL) {
  223.                 short space = 0;
  224.  
  225.                 for (ref = GetHead(&depCmdList->dc_RhsList); r == 0 && ref; ref = GetSucc(&ref->rn_Node))
  226.                 PutCmdListSym(&var->var_CmdList, ref->rn_Node.ln_Name, &space);
  227.             }
  228.             SomeWork = 1;
  229.             if (ExecuteCmdList(dep, depCmdList->dc_CmdList) > 5) {
  230.                 r = -1;
  231.             }
  232.             } else {
  233.             masterForce = 2;
  234.             }
  235.         }
  236.         if (dep->dn_Time == 0 || (long)(*pt -  dep->dn_Time) > 0)
  237.             dep->dn_Time = *pt;
  238.         }
  239.     }
  240.     if (GetHead(&dep->dn_DepCmdList) == NULL) {
  241.         if (statok)
  242.         *pt = sbuf.st_mtime;
  243.     }
  244.     if (masterForce)
  245.         *pt = 0;
  246.     dep->dn_Time = *pt;
  247.     dbprintf(("FINAL %s statok=%d time=%08lx\n", dep->dn_Node.ln_Name, statok, *pt));
  248.     } else {
  249.     *pt = dep->dn_Time;
  250.     dbprintf(("DONE-ALREADY %s time=%08lx\n", dep->dn_Node.ln_Name, *pt));
  251.     }
  252.     return(r);
  253. }
  254.  
  255.